<?php

use console\db\channels\TransferTableMigration;
use common\helpers\StringHelper;

/**
 * 用户认证信息转移
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m300000_000011_t_user_authent extends TransferTableMigration {

    /**
     * @inheritdoc
     */
    public $sourceDb = 'olddb';

    /**
     * @inheritdoc
     */
    public function safeUp() {
        $tableName = '{{%user_authent}}';
        foreach ($this->getTableAlterColumns() as $column => $type) {
            $this->alterColumn($tableName, $column, $type);
        }
        $this->dropIndex('idcardno', '{{%user_authent}}');
        parent::safeUp();
    }

    /**
     * 修改认证表字段
     * @inheritdoc
     */
    public function getTableAlterColumns() {
        return [
            'idcardno_hash' => $this->char(255)->comment('加密证件号（可逆）'),
            'passport_front' => $this->char(255)->comment('证件照正面'),
            'passport_reverse' => $this->char(255)->comment('证件照反面'),
            'photo' => $this->char(255)->comment('人像照'),
            'mark' => $this->text()->comment('审核备注'),
        ];
    }

    /**
     * @inheritdoc
     */
    public function getTransferOptions() {
        return [
            'table' => [
                'user_authent' => 'member_authent',
            ],
            'columns' => [
                'user_id' => 'uid',
                'realname',
                'gender' => function ($data) {
                    $idcard = preg_replace('/[^0-9|x|X]/', '', $data['idcardno']);
                    if (!StringHelper::isIdcardNumber($idcard)) {
                        return 0;
                    }
                    $pos = strlen($idcard) == 15 ? -3 : -2;
                    return 2 - substr($idcard, $pos, 1) % 2;
                },
                'ict_id' => 'ictid',
                'idcardno' => function($data) {
                    return preg_replace('/[^0-9|x|X]/', '', $data['idcardno']);
                },
                'photo' => function($data) {
                    if ($data['photo']) {
                        return '/uploads/' . ltrim($data['photo'], '/');
                    }
                    return '';
                },
                'birthday' => function($data) {
                    $idcard = preg_replace('/[^0-9|x|X]/', '', $data['idcardno']);
                    if (!StringHelper::isIdcardNumber($idcard)) {
                        return null;
                    }
                    return strlen($idcard) == 18 ? substr($idcard, 6, 8) : '19' . substr($idcard, 6, 6);
                },
                'mark' => function($data) {
                    $reason = $data['reason'] ? $data['reason'] . "\n" : '';
                    $idcard = preg_replace('/[^0-9|x|X]/', '', $data['idcardno']);
                    if (!StringHelper::isIdcardNumber($idcard)) {
                        $reason .= "[" . date('Y-m-d H:i:s') . "]身份证号不正确";
                    }
                    return $reason;
                },
                'status' => function($data) {
                    $idcard = preg_replace('/[^0-9|x|X]/', '', $data['idcardno']);
                    if (!StringHelper::isIdcardNumber($idcard)) {
                        return -1;
                    }
                    if ($data['status'] == 1) {
                        return 10;
                    }
                    return $data['status'];
                },
                'audit_at' => 'authtime',
                'created_at' => function($data) {
                    if ($data['authtime']) {
                        return $data['authtime'] - rand(3600, 86400);
                    }
                    $reason = $data['reason'];
                    if ($reason) {
                        $datetime = substr($reason, 1, 19);
                        return strtotime($datetime);
                    }
                    return 0;
                },
                'updated_at' => function($data) {
                    if ($data['authtime']) {
                        return $data['authtime'];
                    }
                    $reason = $data['reason'];
                    if ($reason) {
                        $list = explode("\n", $reason);
                        $last = array_pop($list);
                        $datetime = substr($last, 1, 19);
                        return strtotime($datetime);
                    }
                    return 0;
                },
            ],
        ];
    }

}
